package com.youjia.controller;

import com.youjia.domain.User;
import com.youjia.service.UserService;
import com.youjia.vo.PageResult;
import com.youjia.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //用户登录
    @PostMapping("/login")
    public SysResult loginByUP(@RequestBody User user) {
        SysResult sysResult = userService.loginByUP(user);
        if(sysResult != null) {
            return sysResult;
        } else {
            return SysResult.fail();
        }
    }
    //修改密码
    @PostMapping("/updatePwd")
    public SysResult updatePwd(@RequestBody User user) {
        Boolean flag = userService.updatePwd(user);
        if(flag) {
            return SysResult.success();
        } else {
            return SysResult.fail();
        }
    }
    //注册
    @PostMapping("/register")
    public SysResult insertUser(@RequestBody User user) {
        Boolean flag = userService.insertUser(user);
        if(flag) {
            return SysResult.success();
        } else {
            return SysResult.fail();
        }
    }
    @GetMapping("/list")
    public SysResult getUserByPage(PageResult pageResult) {
        pageResult = userService.getUserList(pageResult);
        return SysResult.success(pageResult);
    }
    @GetMapping("/{id}")
    public SysResult getUserById(@PathVariable Integer id ) {
        User user = userService.getUserById(id);
        return SysResult.success(user);
    }
    @PutMapping("/status/{id}/{status}")
    public SysResult updateStatus(User user) {
        userService.updateStatus(user);
        return SysResult.success();
    }
    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user) {
        userService.addUser(user);
        return SysResult.success();
    }
    @DeleteMapping("/{id}")
    public SysResult deleteUser(@PathVariable Integer id) {
        userService.deleteUser(id);
        return SysResult.success();
    }
    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return SysResult.success();
    }
}
